home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / temgen.lha / Temgen / tg-0.11 / tgmain.c < prev    next >
C/C++ Source or Header  |  2002-12-18  |  5KB  |  229 lines

  1. #include "alloc.h"
  2. #include "atom.h"
  3. #include "db.h"
  4. #include "y.tab.h"
  5. #include "sysdefs.h"
  6. #include "generator.h"
  7. #include "errtab.h"
  8. #include "srctab.h"
  9. #include "output.h"
  10. #include "use.h"
  11. #include "version.h"
  12.  
  13. extern int  yylex();
  14. extern int  yyparse();
  15.  
  16. extern FILE     *yyin;
  17. extern int       lineno;
  18.  
  19. const char      *curfile = NULL;
  20. int              curfilen = 0;
  21. struct txttab   *text_table = NULL;
  22. struct lintab   *line_table = NULL;
  23. int              errraised = 0;
  24. extern char     *errmsg;
  25. static       int status = 0;
  26. int              debugger = 0;
  27.  
  28. int yywrap()
  29. {
  30.     return 1;
  31. }
  32.  
  33. int yyerror( const char *s )
  34. {
  35.     errraised = lineno;
  36.     /* save_error( curfile, lineno, errmsg ); 
  37.        --- save_error is called in tg.y ! */
  38.     return 0;
  39. }
  40.  
  41. #if 0
  42. void dumpdata( void )
  43. {
  44.     int i;
  45.  
  46.     for ( i=0; i<=lt_maxindex( line_table ); i++ ) {
  47.         struct command *c;
  48.  
  49.         c = (struct command*)lt_get( line_table, i );
  50.         if ( c ) {
  51.             printf( "%d:", i );
  52.             dump_cmd( i, c );
  53.         }
  54.     }
  55. }
  56. #endif
  57.  
  58. void start_file( const char *fname ) 
  59. {
  60.     if ( text_table ) free_txttab( text_table );
  61.     if ( line_table ) free_lintab( line_table );
  62.     text_table = new_txttab( 4096, 4096 );
  63.     line_table = new_lintab( 4096, 4096 );
  64.     curfilen = atom( fname );
  65.     yyin = fopen( fname, "r" );
  66.     if ( !yyin ) {
  67.         fprintf( stderr, "Error opening %s\n", fname );
  68.         exit( 1 );
  69.     }
  70.  
  71.     curfile = fname;
  72.     errraised = 0;
  73.     lineno = 1;
  74. }
  75.  
  76. struct sourcefile *parsefile( const char *fname )
  77. {
  78.     struct sourcefile *f;
  79.  
  80.     f = (struct sourcefile*)MALLOC( sizeof(*f) );
  81.     if ( f ) {
  82.         start_file( fname );
  83.         yyparse();
  84.         
  85.         if ( errraised ) {
  86.             FREE( f );
  87.             f = NULL;
  88.             status++;
  89.         } else {
  90.             f->fname = curfilen;
  91.             f->tt = text_table;
  92.             f->lt = line_table;
  93.             text_table = NULL;
  94.             line_table = NULL;
  95.         }
  96.     }
  97.  
  98.     return f;
  99. }
  100.  
  101. /* table of sourcefile*'s */
  102. struct lintab *source = NULL;
  103.  
  104. static int run_file( struct sourcefile *sf )
  105. {
  106.     int cur = 0;
  107.     int maxndx;
  108.     
  109.     if ( !sf ) return -1;
  110.     maxndx = sf->lt ? lt_maxindex( sf->lt ): -1;
  111.     
  112.     while( cur>=0 && cur<=maxndx ) {
  113.         struct command *c;
  114.         c = lt_get( sf->lt, cur );
  115.         cur = run_cmd( cur, c, sf );
  116.     }
  117.     
  118.     return 0;
  119. }
  120.  
  121. static void longopt( const char *str )
  122. {
  123.         if ( !strcmp( str, "version" )) {
  124.                 printf( version_message, VERSION );
  125.                 exit( 0 );
  126.         }
  127.         if ( !strcmp( str, "help" )) {
  128.                 printf( help_message, VERSION );
  129.                 exit( 0 );
  130.         }
  131. }
  132.  
  133. static void load( const char *fname )
  134. {
  135.     struct sourcefile *sf; 
  136.     
  137.     sf = parsefile( fname );
  138.     if ( sf ) {
  139.         int res;
  140.  
  141.         if ( !source ) source = new_lintab( 16, 16 );
  142.         if ( !source ) {
  143.             fprintf( stderr, "Memory allocation error\n" );
  144.             exit( 1 );
  145.         }
  146.  
  147.         lt_set( source, lt_maxindex(source)+1, sf );
  148.         res = regsrc( sf );
  149.         if ( res && res == -100 ) {
  150.             fprintf( stderr, "Memory allocation error\n" );
  151.             exit( 1 );
  152.         }
  153.     }
  154. }
  155.  
  156. static int source_find( int fname )
  157. {
  158.     int i;
  159.     struct sourcefile *sf;
  160.     
  161.     for ( i=0; i<=lt_maxindex(source); i++ ) { 
  162.         sf = (struct sourcefile*)lt_get( source, i );
  163.         if ( sf->fname == fname ) return 1;
  164.     }
  165.     
  166.     return 0;
  167. }
  168.  
  169. int main( int argc, char *argv[] )
  170. {
  171.     extern int yydebug;
  172.     int i;
  173.  
  174.     for ( i=1; i<argc; i++ ) {
  175.         if ( argv[i][0] == '-' ) {
  176.             switch( argv[i][1] ) {
  177.                 case 'd':
  178.                     debugger = 1;
  179.                     break;
  180.                 case 'h':
  181.                     printf( help_message, VERSION );
  182.                     exit( 0 );
  183.                 case 'y':
  184.                     yydebug = 1;
  185.                     break;
  186.                 case '-':
  187.                     longopt( argv[i]+2 );
  188.                     break;
  189.                 default:
  190.                     fprintf( stderr, "Unrecognized command, use tg --help\n" );
  191.             }
  192.  
  193.             continue;
  194.         }
  195.  
  196.         load( argv[i] );
  197.     }
  198.  
  199.     reset_use();
  200.     while( 1 ) {
  201.         int f;
  202.         f = next_use();
  203.         if ( f <= 0 ) break;
  204.         if ( !source_find(f) )
  205.             load( atom_name(f) );
  206.     }
  207.     
  208.     if ( status ) {
  209.         while( 1 ) {
  210.             const char *msg = next_errmsg();
  211.             if ( !msg ) break;
  212.             fprintf( stderr, "%s\n", msg );
  213.         }
  214.     }
  215.     else {
  216.         for ( i=0; i<=lt_maxindex(source); i++ ) 
  217.             if ( run_file( (struct sourcefile*)lt_get( source, i )) ) break;
  218.         
  219.         status = closeout();
  220.         if ( status ) {
  221.                 char buf[ 256 ];
  222.                 snprintf( buf, sizeof(buf), "write error: %s", strerror(errno) );
  223.                 fatal( buf );
  224.         }
  225.     }
  226.     
  227.     return status;
  228. }
  229.